"""tuntun URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.urls import path
from django.contrib import admin
from django.conf.urls import url, include
from rest_framework_jwt.views import obtain_jwt_token
import os
from common.views.base_views import BaseAPIView
from rest_framework.response import Response
from tuntun.settings import BASE_DIR
import time
# from extra_apps import xadmin
from tuntun.settings import STATIC_ROOT, STATIC_URL
from django.conf.urls.static import static

class UpdateProjectView(BaseAPIView):
    permission_classes = ()
    def post(self, request, *args, **kwargs):
        project = request.GET.get('project')
        log = '/tmp/githooklog_tuntun'
        cmd_return = 0
        cmd = '-'
        if project == 'tuntun':
            cmd = 'cd %s && git pull > %s' % (BASE_DIR, log)
            cmd_return += os.system(cmd)
            # os.system('supervisorctl restart tuntun_dev:  &')
        with open(log) as f:
            lines = f.readlines()
        lines.append(time.strftime('%Y:%m:%d %H:%M:%S'))
        lines.append(str(cmd_return))
        lines.append(cmd)
        return Response(lines)

urlpatterns = [
    path('admin/doc/', include('django.contrib.admindocs.urls')),
    path('admin/', admin.site.urls),
    # path('xadmin/', xadmin.site.urls),
    path('message/', include("apps.message.urls")),
    path('user/', include("apps.user.urls")),
    path('product/', include("apps.product.urls")),
    path('order/', include("apps.order.urls")),
    path('tea-repo/', include('apps.tea_repo.urls')),  # 茶仓接口
    path('repo/', include("apps.repo.urls")),
    path('system/', include('apps.system.urls')),  # 系统接口
    path('storage/', include('apps.storage.urls')),
    path('adm/', include('apps.adm.urls')),
    # jwt的认证接口
    url(r'^login/', obtain_jwt_token),
    path('update_project', UpdateProjectView.as_view()),
]


urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT, show_index=True)
